﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;

namespace PaiKe.Web
{
    public partial class Index : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void Lin()
        {
            string sUrl = "http://www.zjch.gov.cn";
            WebClient wClient = new WebClient();
            wClient.Credentials = CredentialCache.DefaultCredentials;
            string pageData = Encoding.UTF8.GetString(wClient.DownloadData(sUrl));
            string r = HttpUtility.HtmlDecode("<div(\\s*)style =\"(.+?)\">(\\s*)•(\\s*)<a href='(?<url>.+?)'(\\s*)target='_blank'(\\s*)title='(?<title>.+?)'>.+?</a>(\\s*)</div>");
            string r2 = HttpUtility.HtmlDecode("<div(\\s*)class=\"consent_detail_row4\"([\\s\\S]+?)>(\\s*)来源：(?<source>.+?)(\\s*)作者：(?<author>.+?)(\\s*)发布日期：(?<date>.+?)(\\s*)点击量：<script(\\s*)src='(?<viewnum>.+?)'></script>(\\s*)</div>(\\s*)<div(\\s*)class=\"consent_detail_row5\"([\\s\\S]+?)id=\"fSize\">(?<context>[\\s\\S]+?)</div>(\\s*)<div(\\s*)class=\"consent_detail_row6\"");
            Regex re = new Regex(r, RegexOptions.IgnoreCase);
            Match match = re.Match(pageData);
            if (match.Success)
            {
                MatchCollection mcs = re.Matches(pageData);
                foreach (Match me in mcs)
                {
                    //二次匹配详细内容
                    pageData = Encoding.UTF8.GetString(wClient.DownloadData(sUrl + me.Groups["url"].Value));
                    re = new Regex(r2, RegexOptions.IgnoreCase | RegexOptions.Multiline);
                    match = re.Match(pageData);
                    if (match.Success)
                    {
                        string url = sUrl + me.Groups["url"].Value;
                        string title = me.Groups["title"].Value;
                        string source = match.Groups["source"].Value;
                        string author = match.Groups["author"].Value;
                        string date = match.Groups["date"].Value;
                        string viewNumUrl = match.Groups["viewNumUrl"].Value;
                        string context = Regex.Replace(match.Groups["context"].Value, "<img(.*?)src=\"(.*?)\">", "<img$1src=\"" + sUrl + "$2\">", RegexOptions.Compiled | RegexOptions.IgnoreCase);
                    }
                }
            }
        }
    }
}